package no.somenvm.dreamplanners

import com.gitee.planners.api.KeyBindingAPI
import com.gitee.planners.api.ProfileAPI
import com.gitee.planners.api.ProfileAPI.plannersProfile
import com.gitee.planners.api.event.PluginReloadEvents
import com.gitee.planners.api.event.player.PlayerProfileLoadedEvent
import com.gitee.planners.core.player.PlayerSkill
import no.somenvm.dreamplanners.DreamPlanners.debug
import no.somenvm.dreamplanners.DreamPlanners.log
import no.somenvm.dreamplanners.DreamPlanners.reload
import no.somenvm.dreamplanners.util.sendVariable
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import priv.seventeen.artist.dreampainter.api.DreamPainterScreenAPI
import priv.seventeen.artist.dreampainter.api.screen.IScreen
import priv.seventeen.artist.dreampainter.event.ClientCustomPacketEvent
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.submit
import taboolib.module.nms.getName

object SkillHud {
    var isEnable = true

    lateinit var hud: IScreen

    //清空图标
    private fun clearAllIcon(player: Player){
        val regs = ProfileAPI.get(player.uniqueId).getRegistrySkill()
        regs.toMap().map { it.value.binding?.id }.forEach {
            it?.let {
                DreamPainterScreenAPI.sendIconItemStack(player,it,null)
            }
        }
    }

    //向客户端发送技能图标
    private fun sendIcon(player : Player, skill: PlayerSkill, key: String? = null){
        val icon = KeyBindingAPI.createIconFormatter(player,skill).build()
        val id = key ?: skill.binding?.id ?: return
        DreamPainterScreenAPI.sendIconItemStack(player,id,icon)
        debug { log.sendMessage("发送图标: &e$id, &e${icon.getName()}") }
    }

    @SubscribeEvent
    fun e(e: PlayerProfileLoadedEvent){
        updateIcon(e.player)
    }

    @SubscribeEvent
    fun e(e: PluginReloadEvents.Post){
        reload()
    }

    //更新技能图标
    fun updateIcon(player: Player?= null){
        if (player == null){
            Bukkit.getOnlinePlayers().forEach {p ->
                if (p.isOnline) {
                    p.plannersProfile.getRegistrySkill().toMap().forEach {
                        sendIcon(p,it.value)
                    }
                }
            }
            return
        }
        player.plannersProfile.getRegistrySkill().toMap().forEach {
            sendIcon(player,it.value)
        }
    }

//    @SubscribeEvent
//    fun e (e: PlayerCastSkillEvents.Post){
//        updateSkillCoolDown(e.player,e.skill)
//    }

    fun sendCoolDown(player: Player, skill: PlayerSkill){
        skill.binding?.id?.let {
            // TODO()
            //等待PL2写冷却功能
        }
    }

    fun sendCoolDown(player: Player, skill: PlayerSkill ,cd: Long){
        hud.sendVariable(player,"CD_${skill.immutable.id}","DynamicNumber.create(${cd.toDouble()},0.0,${cd.toDouble()})")
    }

    @SubscribeEvent
    fun clientCastSkill(e : ClientCustomPacketEvent){
        if (e.id != "DreamPlanners") return
        if (e.getData(0).list.getOrNull(0) == "CastSkill") {
            e.getData(0).list.getOrNull(1)?.let { key ->
                //TODO()
            }
        }
    }
}